package org.demo.szml.userservice.utils;


import de.mkammerer.argon2.Argon2;
import de.mkammerer.argon2.Argon2Factory;


/**
 * @description: Argon2密码加密工具类
 * @date: 2025/6/17 15:00
 * @version: 1.0
 */
public class Argon2Util {
    private static final Argon2 ARGON2 = Argon2Factory.create();

    // 迭代次数
    private static final int ITERATIONS = 3;
    // 内存成本（16MB）
    private static final int MEMORY = 16384;
    // 并行度
    private static final int PARALLELISM = 1;

    /**
     * 加密密码
     */
    public static String encrypt(String rawPassword) {
        try {
            return ARGON2.hash(ITERATIONS, MEMORY, PARALLELISM, rawPassword.toCharArray());
        } finally {
            // 清除敏感数据（防止内存泄漏）
            ARGON2.wipeArray(rawPassword.toCharArray());
        }
    }

    /**
     * 验证密码
     */
    public static boolean verify(String encodedPassword, String rawPassword) {
        try {
            return ARGON2.verify(encodedPassword, rawPassword.toCharArray());
        } finally {
            ARGON2.wipeArray(rawPassword.toCharArray());
        }
    }
}
